<?php
class Action_Administration_Module extends CMF_Krypton_Core_Action
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->loadLanguage( 'administration.module', 'module' );
		$this->addAction( 'addModuleDetails' );
		$this->addAction( 'editModuleDetails' );
		$this->addAction( 'editModuleFiles' );
		$this->addAction( 'editModuleRelations' );
		$this->addAction( 'editModuleDatabase' );
		$this->addAction( 'editModulePages' );
	}

	public function act()
	{
		$moduleId	= $this->request->get( 'moduleId' );
		$action		= $this->request->get( 'action' );
		switch( $action )
		{
			case 'createArchive':
				$this->createArchive( $moduleId );
				break;
			case 'removeInstalled':
				$this->removeInstalled( $moduleId );
				break;
		}
	}

	protected function addModuleDetails()
	{
		try
		{
			$moduleId	= $this->request->get( 'add_module_id' );
			$data		= array(
				'version'		=> $this->request->get( 'add_module_version' ),
				'label'			=> $this->request->get( 'add_module_label' ),
				'description'	=> $this->request->get( 'add_module_description' ),
				'author'		=> $this->request->get( 'add_module_author' ),
				'company'		=> $this->request->get( 'add_module_company' ),
			);
			$length	= Logic_Module::addConfig( $moduleId, $data );
			$this->messenger->noteSuccess( 'module added' );
			$url	= './?link='.$this->request->get( 'link' ).'&moduleId='.$moduleId.'&action=editModule';
			$this->restart( $url );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'module' );
		}
	}

	protected function createArchive( $moduleId )
	{
		if( $moduleId )
		{
			try
			{
				$length	= Logic_Module::createArchiveFromModule( $moduleId );
				$this->messenger->noteSuccess( 'archive created ('.$length.'B)' );
			}
			catch( Exception $e )
			{
				$this->handleException( $e );
			}
		}
	}

	protected function editModuleDetails()
	{
		$moduleId	= $this->request->get( 'moduleId' );
		if( !$moduleId )
			return;
		try
		{
			$data	= array(
				'id'			=> $this->request->get( 'edit_module_id' ),
				'version'		=> $this->request->get( 'edit_module_version' ),
				'label'			=> $this->request->get( 'edit_module_label' ),
				'description'	=> $this->request->get( 'edit_module_description' ),
				'author'		=> $this->request->get( 'edit_module_author' ),
				'company'		=> $this->request->get( 'edit_module_company' ),
			);
			$length	= Logic_Module::editConfig( $moduleId, $data );
			$this->messenger->noteSuccess( 'module details updated' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	protected function editModuleFiles()
	{
		$moduleId	= $this->request->get( 'moduleId' );
		if( !$moduleId )
			return;
		try
		{
			$data	= array(
				'files'	=> array(
					'classes'		=> explode( "\n", trim( $this->request->get( 'edit_module_classes' ) ) ),
					'html'			=> explode( "\n", trim( $this->request->get( 'edit_module_html' ) ) ),
					'languages'		=> explode( "\n", trim( $this->request->get( 'edit_module_languages' ) ) ),
					'templates'		=> explode( "\n", trim( $this->request->get( 'edit_module_templates' ) ) ),
					'images'		=> explode( "\n", trim( $this->request->get( 'edit_module_images' ) ) ),
				)
			);
			$length	= Logic_Module::editConfig( $moduleId, $data );
			$this->messenger->noteSuccess( 'module files updated' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	protected function editModuleDatabase()
	{
		$moduleId	= $this->request->get( 'moduleId' );
		if( !$moduleId )
			return;
		try
		{
			$data	= array(
				'sql'	=> array(
					'install'		=> trim( $this->request->get( 'edit_module_install' ) ),
					'uninstall'		=> trim( $this->request->get( 'edit_module_uninstall' ) ),
				)
			);
			$length	= Logic_Module::editConfig( $moduleId, $data );
			$this->messenger->noteSuccess( 'module database updated' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	protected function editModuleRelations()
	{
		$moduleId	= $this->request->get( 'moduleId' );
		if( !$moduleId )
			return;
		try
		{
		#	...
			$this->messenger->noteSuccess( 'module relations updated' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e );
		}
	}

	protected function removeInstalled( $moduleId )
	{
		if( $moduleId )
		{
			try
			{
				$length	= Logic_Module::uninstallModule( $moduleId );
				$this->messenger->noteSuccess( 'module removed' );
			}
			catch( Exception $e )
			{
				$this->handleException( $e );
			}
		}
	}
}
?>
